<?php
/**
 * File : ModelUser.php
 * Created by Ludovic Reenaers
 * Created on  20 oct. 2010
 * lreenaers@hotmail.com
 */
class ModelUser extends Model{
	function password(){
		try{
			$url = $_SESSION['config']->controler.'?/homepage/show/';
			if(isset($_POST['email']) && isset($_POST['candidate']) && isset($_POST['password']) && isset($_POST['password2'])){
				if($_POST['password'] == $_POST['password2'] && Validator::validatePassword($_POST['password'])){
					if($_POST['candidate'] == md5($_SESSION['tmp_user']->getEmail().$_SESSION['tmp_user']->getPwsHash()) && $_SESSION['tmp_user']->getEmail() == $_POST['email']){
						$_SESSION['tmp_user']->setPwsHash(md5($_POST['password']),md5($_POST['password2']));
						$usrDAO = $this->daoFactory->getUserDAO();
						$bool = $usrDAO->update($_SESSION['tmp_user']);
						if(!$bool){
							throw new TransactionException($this->translator->failedUpdate);
						}else{
							$this->stopFormSession();
							unset($_SESSION['tmp_user']);
						}
					}
				}else{
					throw new FieldFormatException($this->translator->badFormat." ".$this->translator->pws);
					
				}
			}
		}catch (Exception $e){
			$this->addFormError($e);
			$url = $_SESSION['config']->controler."?/user/reset/".$_POST['email']."/".$_POST['candidate']."/";
		}
		return $url;
	}
	function reset(){
		$candidate = $this->params[4];
		$mail = $this->params[3];
		$usrDAO = $this->daoFactory->getUserDAO();
		$usr = $usrDAO->getByEmail($mail);
		$generator = null;
		if(is_a($usr, 'User') && $usr->isActive()){
			if($candidate == md5($usr->getEmail().$usr->getPwsHash())){
				$_SESSION['tmp_user'] = $usr;
				$this->startFormSession();
				$generator = new ResetFormGenerator($this->translator,$this->params,$this->daoFactory);
			}
		}
		return  $generator;
	}
	function insert(){
		try{
			$usrDao = $this->daoFactory->getUserDAO();
			$usrBEAN = new User(0);
			$usrBEAN->setName($_POST['nom']);
			$usrBEAN->setSurname($_POST['prenom']);
			$usrBEAN->setEmail($_POST['email']);
			$usrBEAN->setStreet($_POST['rue']);
			$usrBEAN->setNumber($_POST['numero']);
			$usrBEAN->setZip($_POST['zip']);
			$usrBEAN->setCity($_POST['city']);
			$usrBEAN->setCountry($_POST['country']);
			$lngDAO = $this->daoFactory->getLangueDAO();
			$usrBEAN->setLangue($lngDAO->getById($_POST['lng']));
			list($day,$month,$year)=split('[/,-]',$_POST['ddn']);
			$usrBEAN->setDdn($day,$month,$year);
			if(Validator::validatePassword($_POST["pws"]) && Validator::validatePassword($_POST["pws2"])){
				$usrBEAN->setPwsHash(md5($_POST["pws"]),md5($_POST["pws2"]));
			}else{
				throw new FieldFormatException($this->translator->badFormat." ".$this->translator->pws);
			}

			$securimg = new Securimage();
			$captchabool = $securimg->check($_POST['captcha_code']);
			if(!$captchabool){
				throw new FieldFormatException($this->translator->invalidcaptcha);
			}
			//TODO: if Admin take care of form profile selection info (if has token user#admin)
			if($_SESSION['USER_BEAN']->getProfile()->getLabel()=='anonymous'){
				echo "iiin";
				$profDAO = $this->daoFactory->getProfileDAO();
				$prof = $profDAO->getByLabel('web');
				$usrBEAN->setProfile($prof);
			}
			
			$usrBEAN = $usrDao->add($usrBEAN);
			
			if(!is_a($usrBEAN, 'User')){
				throw new TransactionException($this->translator->failedInsert);
			}else{
				$this->stopFormSession();
				$_SESSION['USER_BEAN'] = $usrBEAN;

			}
		}catch (Exception $e){
			while($key = key($_POST)){
				$this->addFormValue($key,$_POST[$key]);
				next($_POST);
			}
			$this->addFormError($e);
		}
		return "done!";
	}
	function update(){
		try{
			$usrDao = $this->daoFactory->getUserDAO();
			$_SESSION['USER_BEAN']->setName($_POST['nom']);
			$_SESSION['USER_BEAN']->setSurname($_POST['prenom']);
			$_SESSION['USER_BEAN']->setEmail($_POST['email']);
			$_SESSION['USER_BEAN']->setStreet($_POST['rue']);
			$_SESSION['USER_BEAN']->setNumber($_POST['numero']);
			$_SESSION['USER_BEAN']->setZip($_POST['zip']);
			$_SESSION['USER_BEAN']->setCity($_POST['city']);
			$_SESSION['USER_BEAN']->setCountry($_POST['country']);
			$lngDAO = $this->daoFactory->getLangueDAO();
			$_SESSION['USER_BEAN']->setLangue($lngDAO->getById($_POST['lng']));
			list($day,$month,$year)=split('[/,-]',$_POST['ddn']);
			$_SESSION['USER_BEAN']->setDdn($day,$month,$year);
			if(Validator::validatePassword($_POST["pws"]) && Validator::validatePassword($_POST["pws2"])){
				$_SESSION['USER_BEAN']->setPwsHash(md5($_POST["pws"]),md5($_POST["pws2"]));
			}else{
				throw new FieldFormatException($this->translator->badFormat." ".$this->translator->pws);
			}
				
			$securimg = new Securimage();
			$captchabool = $securimg->check($_POST['captcha_code']);
			if(!$captchabool){
				throw new FieldFormatException($this->translator->invalidcaptcha);
			}
			$bool = $usrDao->update($_SESSION['USER_BEAN']);
			if(!$bool){
				throw new TransactionException($this->translator->failedUpdate);
			}else{
				$this->stopFormSession();
			}
		}catch (Exception $e){
			while($key = key($_POST)){
				$this->addFormValue($key,$_POST[$key]);
				next($_POST);
			}
			$this->addFormError($e);
		}
		return "done!";
	}
	function add(){
		$this->startFormSession();
		$generator = new UserFormGenerator($this->translator,$this->params,$this->daoFactory);
		$generator->addChild(new CaptchaGenerator($this->translator,$this->params,$this->daoFactory));
		return $generator;
	}
	function edit(){
		$this->startFormSession();
		
		$generator = new UserFormGenerator($this->translator,$this->params,$this->daoFactory);
		
		$generator->addChild(new CaptchaGenerator($this->translator,$this->params,$this->daoFactory));
		
		return $generator;
	}
	function authenticate(){
		$bool = false;
		$dao = $this->daoFactory->getUserDAO();
		$user = $dao->getByEmail($_POST['email']);
		if(isset($_POST['keep'])){
			setcookie('kaktus[email]', $_POST['email']);
		}
		if ($_SESSION["failedlogin"]>2){	
			$securimg = new Securimage();
			$captchabool = $securimg->check($_POST['captcha_code']);
		}
		try{
			if(is_a($user, 'User') && $user->getPwsHash() == md5($_POST['password']) && $user->getProfile()->getLabel()<>'anonymous' && ($_SESSION["failedlogin"]<=2 or ($_SESSION["failedlogin"] >2 and $captchabool))&& $user->isActive()){
				$this->stopFormSession();
				unset($_SESSION["failedlogin"]);
				$_SESSION["USER_BEAN"] = $user;
				$_SESSION['langue'] = $user->getLangue()->getIso();
				$bool=true;
					
			}else{
				if ($_SESSION["failedlogin"]>2 and !$captchabool){
					throw new FieldFormatException($this->translator->invalidcaptcha);
				}
				if (isset($_SESSION["failedlogin"])){
					$_SESSION["failedlogin"] = $_SESSION["failedlogin"]+1;
				}else{
					$_SESSION["failedlogin"]=1;
				}
				throw new FieldFormatException($this->translator->autherr);
				
			}
		}catch (Exception $e){
			$this->addFormError($e);
			$bool=false;
		}
		return $bool;
	}
	function logout(){
		$bool = false;
		$dao = $this->daoFactory->getUserDAO();
		$user = $dao->getAnonymousUser();
		if(is_a($user, 'User')){
			$_SESSION['USER_BEAN'] = $user;
			$bool=true;
		}
		$_SESSION['langue']= $this->getDefaultLanguageIso();
		return $bool;
	}
}
?>